<template>
  <div id="khxx-djs-list">
    <div>
      <el-select
        style="width: 40vw"
        v-model="selectValue"
        placeholder="请选择客户"
        @change="onSelectChange"
        :disabled="selectDisable"
      >
        <el-option
          v-for="item in selectOptions"
          :key="item.customerId"
          :label="item.customerName"
          :value="item.customerId"
        >
        </el-option>
      </el-select>
      <span style="padding-left: 10px">
        <el-button
          type="primary"
          :disabled="selectDisable || !selectTid"
          @click="clickAddLXR"
          >添加大事件</el-button
        >
      </span>
    </div>
    <br />
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
      highlight-current-row
      height="60vh"
    >
      <el-table-column
        :resizable="false"
        prop="brId"
        label="编号"
        width="50"
        fixed
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="brDate"
        label="日期"
        width="160"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="brPlace"
        label="地点"
        width="200"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="brOutline"
        label="概要"
        width="240"
      >
      </el-table-column>
      <el-table-column
        :resizable="false"
        prop="brInformation"
        label="详细信息"
        width="300"
      >
      </el-table-column>
      <el-table-column :resizable="false" prop="brCustomerId" v-if="false">
      </el-table-column>
      <el-table-column
        :resizable="false"
        fixed="right"
        label="操作"
        width="160"
      >
        <template slot-scope="scope">
          <!-- v-if="scope.row.oppStateId == 1001" -->
          <el-button
            size="mini"
            type="success"
            :disabled="
              scope.row.dpExecutionResults != '' &&
              scope.row.dpExecutionResults != null
            "
            @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            slot="reference"
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      width="40vw"
      :title="changeOrAdd ? '修改大事件' : '添加大事件'"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        label-position="right"
        label-width="110px"
        ref="formLabelAlign"
        :model="formLabelAlign"
        ><!-- :inline="true":自动多行处理 -->
        <el-form-item prop="brId" v-if="false">
          <el-input disabled v-model="formLabelAlign.brId"></el-input>
        </el-form-item>
        <el-form-item
          label="日期"
          prop="brDate"
          :rules="{ required: true, message: '请选择日期!' }"
        >
          <el-date-picker
            style="width: 100%"
            v-model="formLabelAlign.brDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="地点"
          prop="brPlace"
          :rules="{ required: true, message: '请输入地点!' }"
        >
          <el-input
            placeholder="请输入地点"
            v-model="formLabelAlign.brPlace"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="概要"
          prop="brOutline"
          :rules="{ required: true, message: '请输入概要!' }"
        >
          <el-input
            placeholder="请输入概要"
            v-model="formLabelAlign.brOutline"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="详细信息"
          prop="brInformation"
          :rules="{ required: true, message: '请输入详细信息!' }"
        >
          <el-input
            placeholder="请输入详细信息"
            type="textarea"
            v-model.number="formLabelAlign.brInformation"
          ></el-input>
        </el-form-item>
        <el-form-item prop="brCustomerId">
          <el-input
            v-if="false"
            v-model.number="formLabelAlign.brCustomerId"
          ></el-input>
        </el-form-item>

        <div style="padding-top: 10px"></div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('formLabelAlign')">
          确 认
        </el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    
  </div>
</template>

<script>
export default {
  data() {
    return {
      changeOrAdd: false,
      selectValue: null,
      selectOptions: null,
      selectTid: false,
      loading: false,
      selectDisable: true,
      tableData: null,
      dialogFormVisible: false,
      formLabelAlign: {
        brId: null,
        brDate: null, //日期
        brPlace: null, //地点
        brOutline: null, //概要
        brInformation: "", //详细信息
        brCustomerId: this.selectValue, //客户编号
      },
    };
  },
  created: function () {
    this.$axios({
      method: "post",
      crossdomain: true,
      url: "/customer/list",
    }).then((response) => {
      //   console.log(response.data.data[0]); //请求成功返回的数据
      this.selectOptions = response.data.data[0];
      //   console.log(this.tableData);
      this.selectDisable = false;
    });
  },
  methods: {
    onSelectChange(opt) {
      // console.log("select", opt);
      this.loading = true;
      this.selectDisable = true;
      this.$axios({
        method: "post",
        crossdomain: true,
        url: "/business-record/list/" + opt,
      }).then((response) => {
        // console.log(response.data.data[0]); //请求成功返回的数据 列表信息
        this.tableData = response.data.data[0];
        // console.log(this.tableData);
        this.loading = false;
        this.selectDisable = false;
        this.selectTid = true;
      });
    },
    handleDelete(index, row) {
      this.$confirm(`此操作将永久删除条目${row.brId}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$axios({
          method: "delete",
          crossdomain: true,
          url: "/business-record/del/" + row.brId,
        }).then((response) => {
          // console.log(response); //请求成功返回的数据
          if (response.data.type == "OK") {
            this.tableData.splice(index, 1);
            this.$notify({
              title: "删除成功啦",
              type: "success",
              duration: "1000", // 1000ms后关闭
              showClose: false,
            });
            // this.$router.replace({ path: "/" });
          } else {
            this.$notify({
              title: "删除失败惹",
              type: "error",
              message: response.data.msg,
              duration: "4000", // 1000ms后关闭
              showClose: false,
            });
          }
        });
      });

      // console.log("delete", index, row);
    },
    clickAddLXR() {
      // console.log("click add lxr",this.selectValue);// 选择的客户id
      this.changeOrAdd = false;
      this.formLabelAlign = {
        brId: null,
        brDate: null, //日期
        brPlace: null, //地点
        brOutline: null, //概要
        brInformation: "", //详细信息
        brCustomerId: this.selectValue, //客户编号
      };
      this.dialogFormVisible = true;
    },
    handleEdit(index, row) {
      this.changeOrAdd = true;
      this.formLabelAlign = JSON.parse(JSON.stringify(row));
      //   this.formLabelAlign.row = row; // 添加原型数据方便修改
      //   console.log(this.formLabelAlign); //查看转换后
      this.formLabelAlign.accountManager = undefined;
      this.formLabelAlign.customer = undefined;
      this.dialogFormVisible = true;
      // console.log("edit", index, row);
    },
    submitForm(formName) {
      //   console.log(this.formLabelAlign);
      this.$refs[formName].validate((valid) => {
        // console.log("submit:",this.formLabelAlign);
        if (valid) {
          //   console.log(this.changeOrAdd, this.formLabelAlign);
          this.$axios({
            method: this.changeOrAdd ? "put" : "post",
            crossdomain: true,
            url:
              "/business-record/" +
              (this.changeOrAdd
                ? "update?" + this.$qs.stringify(this.formLabelAlign)
                : "add"),
            data: this.$qs.stringify(this.formLabelAlign),
          }).then((response) => {
            // console.log(response); //请求成功返回的数据
            if (response.data.type == "OK") {
              // this.formLabelAlign.row = Object.assign({}, this.formLabelAlign.row, params);
              // console.log(this.formLabelAlign.row);
              this.$notify({
                title: "成功啦",
                type: "success",
                duration: "1000", // 1000ms后关闭
                showClose: false,
              });
              // this.$router.replace({ path: "/" });
              this.dialogFormVisible = false;
              this.onSelectChange(this.selectValue); //刷新列表
            } else {
              this.$notify({
                title: "失败惹",
                message: response.data.msg,
                type: "error",
                duration: "4000", // 1000ms后关闭
                showClose: false,
              });
            }
          });
        } else {
          this.$notify({
            title: "错了哦",
            message: "还有选项没有填对呢",
            type: "error",
            duration: "4000", // 1000ms后关闭
            showClose: false,
          });
          return false;
        }
      });
    },
  },
};
</script>
